package com.run_java_test.test6;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DynamicBeanRegistrar {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    public void registerController2(Class<?> controllerClass) throws Exception {
        if (controllerClass.isAnnotationPresent(RestController.class)) {
            try {
                unregisterController(controllerClass);
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    // 用于存储映射信息
    private Map<RequestMappingInfo, Object> registeredControllers = new HashMap<>();

    public void registerController(Class<?> controllerClass) throws Exception {
        if (controllerClass.isAnnotationPresent(RestController.class)) {
            try {
                // 创建控制器实例
                Object controllerInstance = applicationContext.getAutowireCapableBeanFactory().createBean(controllerClass);
                for (Method method : controllerClass.getDeclaredMethods()) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

                        // 创建 RequestMappingInfo
                        RequestMappingInfo mappingInfo = RequestMappingInfo
                                .paths(requestMapping.value())
                                .methods(requestMapping.method())
                                .produces(requestMapping.produces())
                                .consumes(requestMapping.consumes())
                                .build();

                        // 注册 HandlerMethod
                        requestMappingHandlerMapping.registerMapping(mappingInfo, controllerInstance, method);

                        // 保存注册的信息
                        registeredControllers.put(mappingInfo, controllerInstance);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void registerController3(Class<?> controllerClass) throws Exception {
        if (controllerClass.isAnnotationPresent(RestController.class)) {
            try {
                // 创建控制器实例
                Object controllerInstance = applicationContext.getAutowireCapableBeanFactory().createBean(controllerClass);
                for (Method method : controllerClass.getDeclaredMethods()) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

                        // 创建 RequestMappingInfo
                        RequestMappingInfo mappingInfo = RequestMappingInfo
                                .paths(requestMapping.value())
                                .methods(requestMapping.method())
                                .produces(requestMapping.produces())
                                .consumes(requestMapping.consumes())
                                .build();

                        // 注册 HandlerMethod
                        requestMappingHandlerMapping.registerMapping(mappingInfo, controllerInstance, method);

                        // 保存注册的信息
                        registeredControllers.put(mappingInfo, controllerInstance);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void unregisterController(Class<?> controllerClass) throws Exception {
        if (controllerClass.isAnnotationPresent(RestController.class)) {
            for (Method method : controllerClass.getDeclaredMethods()) {
                if (method.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    RequestMappingInfo mappingInfo = RequestMappingInfo
                            .paths(requestMapping.value())
                            .methods(requestMapping.method())
                            .produces(requestMapping.produces())
                            .consumes(requestMapping.consumes())
                            .build();

                    // 从注册的信息中移除映射
                    Object controllerInstance = registeredControllers.remove(mappingInfo);
                    if (controllerInstance != null) {
                        // 注销映射
                        requestMappingHandlerMapping.unregisterMapping(mappingInfo);
                    }
                }
            }
        }
    }
}
